package edu.npu.GraphQuery;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import edu.npu.GraphQuery.SPGraphQuery.QEdge;

public class ShortestPath {
	List<Long> path;
	
	public ShortestPath() {
		this.path = new ArrayList<Long>();
	}
	//利用路径初始化
	public ShortestPath(ShortestPath sPath) {
		this.path = new ArrayList<Long>(sPath.path);
	}
	//利用节点初始化
	public ShortestPath(long node) {
		this();
		path.add(node);
	}
	
	//最短路径扩展节点
	public void addNode(long node) {
		this.path.add(node);
	}
	//将路径转换为边的集合
	public Set<QEdge> path2edge() {
		Set<QEdge> edgeSet = new HashSet<SPGraphQuery.QEdge>();
		for(int i = 0; i < path.size() - 1; i++) {
			QEdge edge = new QEdge(path.get(i), path.get(i + 1));
			if(!edgeSet.contains(edge))
				edgeSet.add(edge);
		}
		return edgeSet;
	}
	
	@Override
	public int hashCode() {
		List<Long> array = new ArrayList<Long>(this.path);
		Collections.sort(array);
		return array.hashCode();
	}
	@Override
	public boolean equals(Object obj) {
		List<Long> array = new ArrayList<Long>(this.path);
		Collections.sort(array);
		ShortestPath other = (ShortestPath) obj;
		List<Long> array_ = new ArrayList<Long>(other.path);
		Collections.sort(array_);
		if(array.equals(array_))
			return true;
		else
			return false;
	}
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for(long l : path) {
			sb.append(l + ">");
		}
		return sb.substring(0, sb.length() - 1);
	}
	
}
